# 定义一个测试类
import re
import time

from selenium import webdriver
from selenium.common import NoSuchWindowException
from selenium.webdriver.common.by import By

name=[]
telephone=[]
Cell_phone=[]
fax=[]
address=[]
people=[]
class Albb:
    # 定义测试方法-账号不存在
    def login(self):
        driver = webdriver.Chrome()  # 创建浏览器驱动对象
        driver.maximize_window()  # 窗口最大化
        driver.get("https://hongmingsheng.1688.com/page/contactinfo.htm?spm=0.0.wp_pc_common_topnav_38229151.0")   # 打开首页面
        driver.implicitly_wait(10)  # 隐式等待
        # 刷新页面
        driver.refresh()
        time.sleep(2)
        # 获取html数据
        content=driver.page_source
        # 提取商家url
        merchant_name_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[2]'
        merchant_name=driver.find_element(By.XPATH,merchant_name_path).text
        print(merchant_name)
        name.append(merchant_name)
        merchant_telephone_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[1]/div[2]'
        merchant_telephone=driver.find_element(By.XPATH,merchant_telephone_path).text
        print(merchant_telephone)
        telephone.append(merchant_telephone)
        merchant_Cell_phone_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[2]/div[2]'
        merchant_Cell_phone=driver.find_element(By.XPATH,merchant_Cell_phone_path).text
        print(merchant_Cell_phone)
        Cell_phone.append(merchant_Cell_phone)
        merchant_fax_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[3]/div[2]'
        merchant_fax=driver.find_element(By.XPATH,merchant_fax_path).text
        print(merchant_fax)
        fax.append(merchant_fax)
        merchant_address_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[3]/div[4]/div[2]'
        merchant_address=driver.find_element(By.XPATH,merchant_address_path)
        print(merchant_address)
        address.append(merchant_address)
        merchant_people_path='//*[@id="bd_0_container_0"]/div/div[2]/div[1]/div[4]/div[3]'
        merchant_people=driver.find_element(By.XPATH,merchant_people_path)
        print(merchant_people)
        people.append(merchant_people)
        print(content)
        pattern = r'<div title="(.*?)" style="'
        resp = re.findall(pattern,content )
        print(resp)
        name_path = r'line-height: 16px; margin-top: 42px; margin-left: 112px;">(.*?)</div><div style="height: 12px;'
        resp = re.findall(name_path, content)
        print(resp)
        # 现在你可以在新窗口中执行其他的操作了
resp=Albb()
resp.login()
print(name)
print(telephone)
print(Cell_phone)
print(fax)
print(address)